/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *	 
 *	 Copyright (C) 2011
 *   @author christofferP
 */

package com.wirelesstransfer.server.WIFI.handlers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import com.wirelesstransfer.connection.ServerActions;
import com.wirelesstransfer.server.ConnectedClients.ConnectedClient;
import com.wirelesstransfer.server.ConnectedClients.ConnectedClientsHandler;

import android.util.Log;

/**
 * Disconnect a client
 * 
 * 
 * Headers needed:
 * sessionId
 * 
 * @author christofferP
 *
 */
public class DisconnectHandler extends AbstractHandler {
	
	private String TAG = "DisconnectHandler";
	
	private String sessionId;
	private ConnectedClientsHandler connectedClientHandler;
	private ConnectedClient client;

	public void handle(String target,
			Request baseRequest,
			HttpServletRequest request,
			HttpServletResponse response) 
	throws IOException, ServletException {
		Log.v(TAG, "Handling request");
		baseRequest.setHandled(true);

		sessionId = request.getHeader(ServerActions.SESSIONID);
		connectedClientHandler = ConnectedClientsHandler.getInstance();
		client = connectedClientHandler.getClientById(sessionId);
		
		if(client != null){
			client.nextFile();
			connectedClientHandler.removeClient(client);
			
			response.setStatus(HttpServletResponse.SC_OK);
		}
	}

}
